package com.example.shixi.controller;

import com.example.shixi.domain.SysUser;
import com.example.shixi.service.UserService;
import com.example.shixi.util.JSONResult;
import com.example.shixi.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //获取所有用户信息
    @GetMapping("/list")
    public JSONResult list() {

        JSONResult list = userService.list();
        return list;
    }
    //添加用户
    @PostMapping("/logon")
    public JSONResult logon(@RequestBody SysUser user){
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        return userService.save(user);
    }
    //登录
    @PostMapping("/login")
    public JSONResult login(@RequestBody SysUser user){
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        return userService.login(user);
    }
    //删除用户
    @DeleteMapping("/del/{id}")
    public JSONResult del(@PathVariable Long id){
        return userService.del(id);
    }
    //编辑
    @PutMapping("/update")
    public JSONResult update(@RequestBody SysUser user){
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        return userService.update(user);
    }
    //模糊查询真实姓名
    @GetMapping("/query/{pageNum}/{pageSize}")
    public JSONResult query(SysUser user,@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        return userService.query(user,pageNum,pageSize);
    }
}
